using System;
using System.Collections;
using System.Web.UI.WebControls;
// DNN
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
// Codex
using CodexOmegaClassLibrary.Utils;

namespace CodexOmega.Modules.VideoLinker
{
    partial class EditVideoLinker : PortalModuleBase
    {
        #region Private Members

        private int _itemID = Null.NullInteger;

        #endregion

        #region Properties

        public int ItemID
        {
            get { return _itemID; }
            set { _itemID = value; }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            //try
            //{
                // get itemID
                if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    ItemID = DataUtils.getNumericID(Request.QueryString["id"]);
                }

                // CANNOT USE IsPostBack, that's why, it is replaced 
                // by flag, which value is contained in Hidden control
                if (String.IsNullOrEmpty(PostBackFlag.Value))
                {
                    BindData();
                }
            //}
            ////catch (Exception exc) //Module failed to load
            ////{
            ////    Exceptions.ProcessModuleLoadException(this, exc);
            ////}
        }

        protected void cmdCancel_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cmdEditAttributes_Click(System.Object sender, System.EventArgs e)
        {
            Response.Redirect(Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "ManageAttributes", "mid=" + ModuleId.ToString()));
        }

        protected void cmdCreate_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                CreateVideoLink();
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cmdDelete_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                DeleteVideoLink();
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                UpdateVideoLink();
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region Methods

        private void BindData()
        {

            // reset PostBackFlag
            PostBackFlag.Value = "1";


            VideoLinkerController videoLinkerController = new VideoLinkerController();

            // set delete button confirm message
            cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");

            // bind lists
            cboType.DataSource = videoLinkerController.GetVideoTypesList();
            cboType.DataTextField = "key";
            cboType.DataValueField = "value";
            cboType.DataBind();

            cboCategory.DataSource = videoLinkerController.GetVideoCategoriesList();
            cboCategory.DataTextField = "key";
            cboCategory.DataValueField = "value";
            cboCategory.DataBind();


            if (ItemID > 0)
            {
                // get current videoLinkerInfo
                VideoLinkerInfo videoLinkerInfo = videoLinkerController.GetVideoLink(ItemID);


                if (videoLinkerInfo != null)
                {
                    // bind controls on current videoLinkerInfo
                    txtTitle.Text = videoLinkerInfo.VideoTitle;
                    txtDescription.Text = videoLinkerInfo.VideoDescription.Trim();
                    txtCode.Text = videoLinkerInfo.VideoCode.Trim();
                    cboType.SelectedIndex = cboType.Items.IndexOf(cboType.Items.FindByValue(videoLinkerInfo.VideoTypeID.ToString()));
                    cboCategory.SelectedIndex = cboCategory.Items.IndexOf(cboCategory.Items.FindByValue(videoLinkerInfo.VideoCategoryID.ToString()));
                }
                else
                {
                    // go back
                    Response.Redirect(Globals.NavigateURL(), true);
                }
            }
            else
            {
                // show/hide command panels
                panelCommandsCreate.Visible = true;
                panelCommandsUpdate.Visible = false;
                ctlAudit.Visible = false;
            }
        }

        private void CreateVideoLink()
        {
            // get controller
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            // create new object
            VideoLinkerInfo videoLinkerInfo = new VideoLinkerInfo();

            // set object properties
            videoLinkerInfo.VideoCategoryID = Convert.ToInt32(cboCategory.SelectedItem.Value);
            videoLinkerInfo.VideoTypeID = Convert.ToInt32(cboType.SelectedItem.Value);
            videoLinkerInfo.VideoTitle = txtTitle.Text.Trim();
            videoLinkerInfo.VideoDescription = txtDescription.Text.Trim();
            videoLinkerInfo.VideoCode = txtCode.Text.Trim();

            //// Create new video
            videoLinkerController.CreateVideoLink(videoLinkerInfo);

            // refresh cache
            SynchronizeModule();

            // Redirect back to the portal home page
            this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
        }

        private void UpdateVideoLink()
        {
            // get current object
            VideoLinkerController videoLinkerController = new VideoLinkerController();
            VideoLinkerInfo videoLinkerInfo = videoLinkerController.GetVideoLink(ItemID);

            if (videoLinkerInfo != null)
            {
                // set properties
                videoLinkerInfo.VideoCategoryID = Convert.ToInt32(cboCategory.SelectedItem.Value);
                videoLinkerInfo.VideoTypeID = Convert.ToInt32(cboType.SelectedItem.Value);
                videoLinkerInfo.VideoTitle = txtTitle.Text.Trim();
                videoLinkerInfo.VideoDescription = txtDescription.Text.Trim();
                videoLinkerInfo.VideoCode = txtCode.Text.Trim();

                // update the content within the VideoLinker table
                videoLinkerController.UpdateVideoLink(videoLinkerInfo);

                // refresh cache
                SynchronizeModule();
            }

            //Redirect back to the portal home page
            this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
        }

        private void DeleteVideoLink()
        {
            //Only attempt to delete the item if it exists already
            if (!Null.IsNull(ItemID))
            {
                // get controller
                VideoLinkerController videoLinkerController = new VideoLinkerController();

                // delete object
                videoLinkerController.DeleteVideoLink(ItemID);

                //refresh cache
                SynchronizeModule();
            }

            this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
        }

        #endregion

    }
}

